<html>
<head><meta charset="utf-8"><title>Type aliases in inlay hints · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html">Type aliases in inlay hints</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="237804770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237804770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237804770">(May 07 2021 at 11:15)</a>:</h4>
<p><a href="/user_uploads/4715/gIwU9KkyLkSz4N3zv_Przv2X/image.png">image.png</a> </p>
<div class="message_inline_image"><a href="/user_uploads/4715/gIwU9KkyLkSz4N3zv_Przv2X/image.png" title="image.png"><img src="/user_uploads/4715/gIwU9KkyLkSz4N3zv_Przv2X/image.png"></a></div><p>One of those features I want to do, but have no clues how, due to zero knowledge on how things are supposed to work below <code>hir</code>.<br>
Could somebody give me code pointers and high-level explanations on how to tackle this?</p>
<p>I believe, the end user API has to be somewhere on the <code>source_analyzer.rs</code>, where all <code>type_of_*</code> calls should return type aliases instead of a "raw" type as we do now?</p>
<p>It feels like I need to store the alias info into a cache(?) during lowering(???) along with their visibility and look it up before returning a <code>type_of_*</code>?<br>
What about funny cases generic type aliases?</p>



<a name="237806433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237806433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237806433">(May 07 2021 at 11:28)</a>:</h4>
<p>this is quite non-trivial <span aria-label="grimacing" class="emoji emoji-1f62c" role="img" title="grimacing">:grimacing:</span> the first problem is representing this at all. There's currently no way of representing an unresolved type alias as a type, so the two options are a) add it to the Chalk type system, or b) introduce our own intermediate type representation that we transform Chalk types from and back.</p>



<a name="237806551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237806551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237806551">(May 07 2021 at 11:29)</a>:</h4>
<p>a) is something that's actually been considered (basically it would mean implementing "lazy normalization" for type aliases, instead of the eager lowering we have right now), but it's obviously a somewhat deep change in the type system</p>



<a name="237806752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237806752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237806752">(May 07 2021 at 11:31)</a>:</h4>
<p>b) is something I've also wanted to do for other reasons and even started, but stopped for various reasons. I still think it's probably a good idea though. But it would still leave the problem how to thread the information that something was originally a type alias through the actual type system</p>



<a name="237807358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237807358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237807358">(May 07 2021 at 11:36)</a>:</h4>
<p>(to expand on b a bit more, what I'm thinking of is something that's close to how types are written in surface rust (so like <code>TypeRef</code> in that regard), but with resolved IDs instead of names)</p>



<a name="237807853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237807853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237807853">(May 07 2021 at 11:40)</a>:</h4>
<p>here are some slightly more complicated cases to consider:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">foo</span><span class="p">());</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">identity</span><span class="p">(</span><span class="n">foo</span><span class="p">());</span><span class="w"></span>
<span class="k">match</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">z</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="237808094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237808094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237808094">(May 07 2021 at 11:42)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">foo</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="237809774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237809774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237809774">(May 07 2021 at 11:56)</a>:</h4>
<p>We have an issue for this right? Might be good to drop this topic in a comment there(or make an issue about this if we haven't yet)</p>



<a name="237809946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237809946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237809946">(May 07 2021 at 11:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Frust-analyzer/topic/Type.20aliases.20in.20inlay.20hints/near/237808094">said</a>:</p>
<blockquote>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">foo</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</code></pre></div><br>
</p>
</blockquote>
<p>This one looks like a fun one <span aria-label="grimacing" class="emoji emoji-1f62c" role="img" title="grimacing">:grimacing:</span></p>



<a name="237810198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237810198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237810198">(May 07 2021 at 11:59)</a>:</h4>
<p>yeah, I think we do</p>



<a name="237810305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237810305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237810305">(May 07 2021 at 12:00)</a>:</h4>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/issues/1666">https://github.com/rust-analyzer/rust-analyzer/issues/1666</a></p>



<a name="237811024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237811024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237811024">(May 07 2021 at 12:05)</a>:</h4>
<p>I wrote there</p>
<blockquote>
<p>I personally don't think we should treat type aliases as a separate type</p>
</blockquote>
<p>but Niko has since mentioned maybe wanting to do that anyway for other reasons, hence why I mentioned it as possibility a) above</p>



<a name="237815727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20aliases%20in%20inlay%20hints/near/237815727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20aliases.20in.20inlay.20hints.html#237815727">(May 07 2021 at 12:42)</a>:</h4>
<p>Right, so even if you deferred doing this now and it all goes down to the type level, looks like I'm too optimistic here <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> <br>
Thank you for elaborating on this again, I'll link this topic into the issue, as suggested.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>